// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var leftMenu = require("../leftMenu/leftMenu");
	var topMenu = require("../topMenu/topMenu");
	var router = require("../hash/pageRouter");
	var html_financingDetail_financeContract = require("../financing/financingDetail_financeContract.html");
	var html_financingDetail_supervisionContract = require("../financing/financingDetail_supervisionContract.html");
	var html_financingDetail_recieveNotice = require("../financing/financingDetail_recieveNotice.html");
	var html_financingDetail_supervisionInform = require("../financing/financingDetail_supervisionInform.html");
	var css_financeManage = require("../financing/financingManage.css");
	var css_financeDetailInfo = require("../financing/financingDetailInfo.css");
	var js_platform_main = require("./platform_main.js");
	var js_exception = require("../exception/exceptionManage");
	var dialog = require("dialog");
	var tm;
	var validate = require("validate");
	var constant = require("constant")
	var databind = require("databind");
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var left;
	var financeState;
	var financeId;
	var financeNumber;
	var dataFinancingDetail;
	var detainedIds;
	var init = function(page) {
		left = page.params.left || base.getUrlParam("left");
		financeState = page.params.financeState || base.getUrlParam("financeState");
		tm = page.params.tm || base.getUrlParam("tm");
		leftMenu.init(6);
		$(".left").removeClass("none");
		js_platform_main.init(0);
		$(".table-section").removeClass("none");
		$(".footer").removeClass("none");
		$("#main").css("background", "#ffffff");
		$("#leftMenu7").find(".leftMenu-item").removeClass("leftMenu-active");
		$("#leftMenu7 .leftMenu li:first-child").addClass("leftMenu-open");
		$("#leftMenu7").find(".leftMenu-item").eq(left).addClass("leftMenu-active");
		$("#leftMenu7 .leftMenu-disable").removeClass("leftMenu-open");
		$("#leftMenu7 .leftMenu-disable:last-child").addClass("leftMenu-open");
		if(tm!=null){
			js_exception.init();
			topMenu.init(5);
			inquireDetainedApplicationPageList1();
		}else{
			leftMenu.init(6);
			js_platform_main.init(0);
			inquireDetainedApplicationPageList();
		}
		
		initEvent();
		router.init();

	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}
		
		//点击首页
		$(".platform_main").unbind("click").click(function() {
			top.location.href = "../main/platform_main.html";
		});
		
		$("#detainedList .searchButon").unbind("click").click(function() {
			if ($("#detainedList .searchStatus").val() == 1) {
				inquireDetainedApplicationPageList1();
			} else {
				inquireDetainedApplicationPageList();
			}
		});
	};

	//扣货申请列表
	var inquireDetainedApplicationPageList = function() {
		base.fnAjax({
			action: "inquireDetainedApplicationPageList.json",
			oData: {
				detainedIds: $("#detaineds").val().split(','),
				pageIndex: pageIndex,
				pageSize: pageSize
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = $("#detainedListTpl").html();
					var html = juicer(tpl, data);
					$("#detainedListBody").html(html);

					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					base.pageControl(
						$("#detainedPagenation"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							inquireDetainedApplicationPageList();
						});
						$(".detainedApplicationDetail").unbind("click").click(function(){
							router.route("main/detainedApplicationDetail.html?detainedId="+$(this).parents("tr").attr("detainedId"));
						});
						//点击融资编号
					$(".order_num").unbind("click").click(function() {
						var financeIds = [];
						var $this = $(this).parents("tr.tr_style");
						financeIds.push($this.attr("financeId"));
						router.route("financing/financingDetailInfo.html?financeId=" + financeIds, true);
					});
					
					tools.changeWarningColor($("#detainedListBody"), "detainedApplicationDateTime");
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}
	
	//扣货申请列表
	var inquireDetainedApplicationPageList1 = function() {
		base.fnAjax({
			action: "inquireDetainedApplicationPageList.json",
			oData: {
				pageIndex: pageIndex,
				pageSize: pageSize
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = $("#detainedListTpl").html();
					var html = juicer(tpl, data);
					$("#detainedListBody").html(html);

					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					base.pageControl(
						$("#detainedPagenation"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							inquireDetainedApplicationPageList1();
						});
						$(".detainedApplicationDetail").unbind("click").click(function(){
							router.route("main/detainedApplicationDetail.html?detainedId="+$(this).parents("tr").attr("detainedId"));
						});
						//点击融资编号
					$(".order_num").unbind("click").click(function() {
						var financeIds = [];
						var $this = $(this).parents("tr.tr_style");
						financeIds.push($this.attr("financeId"));
						router.route("financing/financingDetailInfo.html?financeId=" + financeIds, true);
					});
					
					tools.changeWarningColor($("#detainedListBody"), "detainedApplicationDateTime");
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}
	return {
		init: init
	};
});